import React, { Component } from 'react'
import store from '../store';
import { changeInputAction, addItemAction, deleteItemAction } from "../store/actionCreators";
import TodoListUI from "./TodoListUI"


class TodoList extends Component {
    constructor(props) {
        super(props)
        this.state = store.getState();
        this.changeInputValue = this.changeInputValue.bind(this);
        this.storeChange = this.storeChange.bind(this);
        this.clickBtn = this.clickBtn.bind(this);
        this.deleteItem = this.deleteItem.bind(this);
        store.subscribe(this.storeChange); //订阅Redux的状态
    }
    render() {
        return (
            <TodoListUI
                inputValue={this.state.inputValue}
                list={this.state.list}
                changeInputValue={this.changeInputValue}
                clickBtn={this.clickBtn}
                deleteItem={this.deleteItem}
            />
        );
    }
    storeChange() {
        this.setState(store.getState());
    }
    // 表单输入
    changeInputValue(e) {
        const action = changeInputAction(e.target.value);
        store.dispatch(action);
    }
    // 添加
    clickBtn() {
        const action = addItemAction();
        store.dispatch(action);
    }
    // 删除
    deleteItem(index) {
        const action = deleteItemAction(index);
        store.dispatch(action);
    }
}

export default TodoList;
